<template>
  <div class="app-container app-content">
    <el-row>
      <el-col :span="24">
        <el-row class="line-bottom" v-show="showSearch">
          <el-col class="form_col">
            <el-form :model="queryParams" ref="queryForm" :size="size" v-show="showSearch">
              <div id="searchFilter" :gutter="10" style="display: flex; flex-wrap: wrap">
                <el-form-item label="型号名称" prop="typeName" label-width="80px">
                  <el-input
                    v-model="queryParams.typeName"
                    placeholder="请输入型号名称"
                    clearable
                    @keyup.enter.native="handleQuery"
                  />
                </el-form-item>
                <el-form-item>
                </el-form-item>
              </div>
            </el-form>
          </el-col>
          <el-col class="form_query">
            <el-button v-show="collapseAble" type="text" @click="shiftCollapseAble" class="text">
              <span>
                {{ fold ? '收起筛选' : '更多筛选' }}
              </span>
            </el-button>
            <el-button type="primary" @click="handleQuery" size="mini">查询</el-button>
            <el-button @click="resetQuery" size="mini">重置</el-button>
          </el-col>
        </el-row>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['basedata:terminaltype:add']"
            >新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['basedata:terminaltype:remove']"
            >批量删除
            </el-button>
          </el-col>
          <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
          <RightDropdown :columns="columns" :showSearch.sync="showSearch" @queryTable="getList" @export="handleExport"
                         :hasPermiExport="['basedata:terminaltype:export']"></RightDropdown>
        </el-row>

        <el-table v-loading="loading" :data="terminaltypeList" border size="mini" ref="table"
                  key="terminalTypeTable"
                  @selection-change="handleSelectionChange" :height="getAvailableHeight">
          <el-table-column type="selection" width="55" align="center"/>
          <!--          <el-table-column label="型号编号" align="center" prop="id" v-if="columns[1].visible"-->
          <!--                           :show-overflow-tooltip="true"/>          -->
          <el-table-column label="型号名称" align="center" prop="typeName" width="140" v-if="columns[0].visible"
                           :show-overflow-tooltip="true"/>
          <!-- (1:户表,2:大用户表) -->
          <el-table-column label="型号分类" align="center" prop="terminalClassify" width="90"
                           v-if="columns[1].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              {{ ['大用户表', '户表'][parseInt(scope.row.terminalClassify) - 1] }}
            </template>
          </el-table-column>
          <el-table-column label="最大量程(m³)" align="center" prop="maxRange" width="110" v-if="columns[2].visible"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="使用年限" align="center" prop="usefulLife" width="90" v-if="columns[3].visible"
                           :show-overflow-tooltip="true"/>
          <!-- (管段式,插入式) -->
          <el-table-column label="安装类型" align="center" prop="installationType" v-if="columns[4].visible"
                           :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.installType" :value="scope.row.installationType"/>
            </template>
          </el-table-column>
          <!-- (1:冷水表,2:热水表) -->
          <el-table-column label="用途类型" align="center" prop="usageField" v-if="columns[5].visible"
                           :show-overflow-tooltip="true">
            <template slot-scope="scope">
              {{ ['冷水表', '热水表表'][parseInt(scope.row.usageField) - 1] }}
            </template>
          </el-table-column>
          <el-table-column label="表类型" align="center" prop="meterType" v-if="columns[6].visible"
                           :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.module_type" :value="scope.row.meterType"/>
            </template>
          </el-table-column>
          <el-table-column label="所属厂家" align="center" prop="supplierId" v-if="columns[7].visible"
                           :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.terminal_supplier" :value="scope.row.supplierId"/>
            </template>
          </el-table-column>
          <el-table-column label="口径大小" align="center" prop="caliber" v-if="columns[8].visible"
                           :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.ldbd_pipe_caliber" :value="scope.row.caliber"/>
            </template>
          </el-table-column>
          <el-table-column label="供电类型" align="center" prop="electricType" v-if="columns[9].visible"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="正常电压(V)" align="center" prop="voltage" v-if="columns[10].visible"
                           :show-overflow-tooltip="true"/>
          <!--          <el-table-column label="通讯类型" align="center" prop="commType" v-if="columns[11].visible"-->
          <!--                           :show-overflow-tooltip="true"/>-->
          <el-table-column label="最小流量(m³/h)" align="center" prop="minFlow" width="120" v-if="columns[12].visible"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="分界流量(m³/h)" align="center" prop="boundaryFlow" width="120"
                           v-if="columns[13].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="常用流量(m³/h)" align="center" prop="commonFlow" width="120"
                           v-if="columns[14].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="过载流量(m³/h)" align="center" prop="overloadFlow" width="120"
                           v-if="columns[15].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="样表图片" align="center" prop="sampleTableImage" width="120">
            <template slot-scope="scope">
              <image-preview v-if="scope.row.sampleTableImage" :src="scope.row.sampleTableImage"></image-preview>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark" v-if="columns[16].visible"
                           :show-overflow-tooltip="true" width="120"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="140">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['basedata:terminaltype:edit']"
                class="text_color"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['basedata:terminaltype:remove']"
                class="text_color"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改型号管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="720px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth" :inline="true">
        <el-form-item label="型号名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入型号名称"/>
        </el-form-item>
        <el-form-item label="最大量程(m³)" prop="maxRange">
          <el-input v-model="form.maxRange" placeholder="请输入最大量程(m³)"/>
        </el-form-item>
        <el-form-item label="使用年限(year)" prop="usefulLife">
          <el-input v-model="form.usefulLife" placeholder="请输入使用年限(year)"/>
        </el-form-item>
        <el-form-item label="表类型" prop="meterType">
          <el-select
            v-model="form.meterType"
            placeholder="请选择表类型"
            clearable
            style="width:202px"
          >
            <el-option
              v-for="dict in dict.type.module_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="所属厂家" prop="supplierId">
          <el-select v-model="form.supplierId" placeholder="请选择所属厂家" clearable style="width: 202px;">
            <el-option
              v-for="dict in dict.type.terminal_supplier"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="口径大小" prop="caliber">
          <el-select v-model="form.caliber" placeholder="请选择口径" style="width: 202px;">
            <el-option
              v-for="dict in dict.type.ldbd_pipe_caliber"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="安装类型" prop="installationType">
          <el-select v-model="form.installationType" placeholder="请选择安装类型" clearable style="width: 202px;">
            <el-option
              v-for="dict in dict.type.installType"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          <!-- <el-input v-model="form.installationType" placeholder="请输入口径大小" /> -->
        </el-form-item>
        <el-form-item label="正常电压(V)" prop="voltage">
          <el-input v-model="form.voltage" placeholder="请输入正常电压（V）"/>
        </el-form-item>
        <el-form-item label="最小流量(m³/h)" prop="minFlow">
          <el-input v-model="form.minFlow" placeholder="请输入最小流量(m³/h)"/>
        </el-form-item>
        <el-form-item label="分界流量(m³/h)" prop="boundaryFlow">
          <el-input v-model="form.boundaryFlow" placeholder="请输入分界流量(m³/h)"/>
        </el-form-item>
        <el-form-item label="常用流量(m³/h)" prop="commonFlow">
          <el-input v-model="form.commonFlow" placeholder="请输入常用流量(m³/h)"/>
        </el-form-item>
        <el-form-item label="过载流量(m³/h)" prop="overloadFlow">
          <el-input v-model="form.overloadFlow" placeholder="请输入过载流量(m³/h)"/>
        </el-form-item>
        <el-form-item label="样表图片" prop="sampleTableImage">
          <file-upload v-model="form.sampleTableImage" :limit="1" :file-size="10"
                       :file-type="['png', 'jpg', 'jpeg']"></file-upload>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTerminaltype,
  getTerminaltype,
  delTerminaltype,
  addTerminaltype,
  updateTerminaltype
} from "@/api/basedata/terminaltype";

export default {
  name: "TerminalType",
  dicts: ['installType', 'module_type', 'belong_department', 'sys_normal_disable', 'ldbd_pipe_caliber', 'terminal_supplier'],
  data() {
    return {
      screenHeight: null,
      size: 'mini',
      collapseAble: false,
      fold: false,
      maxShow: '4',
      // 列信息
      columns: [
        // { key: 1, label: `型号编号`, visible: false },
        {key: 0, label: `型号名称`, visible: true},
        {key: 1, label: `型号分类`, visible: true},
        {key: 2, label: `最大量程(m³)`, visible: true},
        {key: 3, label: `使用年限(year)`, visible: true},
        {key: 4, label: `安装类型`, visible: true},
        {key: 5, label: `用途类型`, visible: true},
        {key: 6, label: `表类型`, visible: true},
        {key: 7, label: `所属厂家`, visible: true},
        {key: 8, label: `口径大小`, visible: true},
        {key: 9, label: `供电类型`, visible: false},
        {key: 10, label: `正常电压`, visible: true},
        {key: 11, label: `通讯类型`, visible: true},
        {key: 12, label: `最小流量(m³/h)`, visible: true},
        {key: 13, label: `分界流量(m³/h)`, visible: true},
        {key: 14, label: `常用流量(m³/h)`, visible: true},
        {key: 15, label: `过载流量(m³/h)`, visible: false},
        {key: 16, label: `备注`, visible: false},
      ],
      labelWidth: '120px',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 型号管理表格数据
      terminaltypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        typeName: null,
        installationType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        typeName: [
          {required: true, message: "型号名称不能为空", trigger: "blur"}
        ],
        terminalClassify: [
          {required: true, message: "型号分类不能为空", trigger: "change"}
        ],
        maxRange: [
          {required: true, message: "最大量程(m³)不能为空", trigger: "blur"}
        ],
        usefulLife: [
          {required: true, message: "使用年限(year)不能为空", trigger: "blur"}
        ],
        installationType: [
          {required: true, message: "安装类型不能为空", trigger: "change"}
        ],
        usageField: [
          {required: true, message: "用途类型不能为空", trigger: "change"}
        ],
      }
    };
  },
  created() {
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - (this.showSearch ? 252 : 200)
    },
  },
  mounted() {
    this.getList()
    // 通过最大显示个数控制展开/折叠
    if (this.maxShow > 0) {
      this.minShowControl()
    }
  },
  methods: {
    // 重绘表格
    // initTable(){
    //   // 调用接口获取到数据后
    //   // let _self = this
    //   this.$nextTick(() => {
    //     window.setTimeout(() => {
    //       this.$refs.table.doLayout()
    //     }, 100)
    //   })
    // },
    // 展开折叠按钮
    shiftCollapseAble() {
      this.fold = !this.fold
      this.minShowControl()
    },
    // 通过maxShow控制元素显示/折叠
    minShowControl() {
      const group = window.document.querySelectorAll(`#searchFilter .el-form-item.el-form-item--${this.size}`)
      const len = group?.length ? group?.length - 1 : 0
      if (this.maxShow < len) {
        group.forEach((item, index) => {
          if (index > this.maxShow - 1 && index < len) {
            item.hidden = !this.fold
          }
        })
        this.collapseAble = true
      } else {
        this.collapseAble = false
      }
    },
    /** 查询型号管理列表 */
    getList() {
      this.loading = true;
      listTerminaltype(this.queryParams).then(response => {
        this.terminaltypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      // this.initTable()
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        typeName: null,
        terminalClassify: null,
        maxRange: null,
        usefulLife: null,
        installationType: null,
        usageField: null,
        meterType: null,
        supplierId: null,
        caliber: null,
        electricType: null,
        voltage: null,
        commType: null,
        minFlow: null,
        boundaryFlow: null,
        commonFlow: null,
        overloadFlow: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加型号管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTerminaltype(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改型号管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTerminaltype(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTerminaltype(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.id || this.ids;
      this.$modal.confirm('是否确认删除型号管理编号为"' + IDs + '"的数据项？').then(function () {
        return delTerminaltype(IDs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basedata/terminaltype/export', {
        ...this.queryParams
      }, `terminaltype_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.option {
  height: auto;
  line-height: 1;
  padding: 0;
  background-color: #fff;
}

.tree {
  padding: 4px 20px;
  font-weight: 400;
}

.form_col {
  width: 80%;

  ::v-deep .el-form {
    width: 100%;
  }

  ::v-deep .el-form-item {
    width: 25%;
  }
}

.form_query {
  width: 20%;
  text-align: right;
}
</style>
